var express = require("express");
var bodyParser = require("body-parser");

var router = express.Router();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
//引入数据模型
var user = require("../model/user");
var comment = require("../model/comment");

router.get("/", urlencodedParser, function(req, res) {
    comment.update({ updateTime: Date.now() });
    comment.find({}).populate("user").exec(function(err, docs) {
        res.render("index", { comments: docs, isAuth: req.isAuthenticated() })
    });
});

router.get("/main/:commentId", urlencodedParser, function(req, res) {
    var commentId = req.params.commentId;
    comment.findById(commentId).populate(["user", "comments.commentUser"]).exec(function(err, doc) {
        console.log(doc);
        console.log(doc.user.username);
        res.render("main", { comment: doc, isAuth: req.isAuthenticated() });
    });

});

module.exports = router;